<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/2
 * Time: 17:24
 */

namespace app\admins\controller;

use app\admins\controller\BaseAdmin;

class Slide extends BaseAdmin
{
    // 首页显示
    public function index()
    {
        $data = $this->db->table('slide')->lists();

        $this->assign('data', $data);
        return $this->fetch();
    }

    // 添加或编辑首屏
    public function add_first()
    {
        $id = (int)input('get.id');
        $slide = $this->db->table('slide')->where(array('id' => $id))->item();


        $this->assign('data', $slide);
        return $this->fetch();
    }

    // 保存(新增或更新)
    public function save()
    {
        $id = (int)input('post.id');  // 如果没有id则默认为0
        $data['type'] = (int)input('post.type');
        $data['ord'] = (int)input('post.ord');
        $data['title'] = trim(input('post.title'));
        $data['url'] = trim(input('post.url'));
        $data['img'] = trim(input('post.img'));
        // 判断字段符合性
        if ($data['title'] == '' || $data['url'] == '' || $data['img'] == '') {
            exit(json_encode(array('code' => 1, 'msg' => '不能为空')));
        }

        // true:存在id传递到后台 为更新  false:新增
        if ($id > 0) {
            $this->db->table('slide')->where(array('id' => $id))->update($data);
        } else {
            $this->db->table('slide')->insert($data);
        }
        exit(json_encode(array('code' => 0, 'msg' => '保存成功')));
    }

    // 删除
    public function delete()
    {
        $id = (int)input('post.id');  // 如果没有id则默认为0
        $this->db->table('slide')->where(array('id' => $id))->delete();
        exit(json_encode(array('code' => 0, 'msg' => '删除成功')));
    }

}